package com.yiwei.zhh.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.IncomeCertBean;
import com.yiwei.zhh.api.bean.UploadImageBean;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.event.IncomeEvent;
import com.yiwei.zhh.util.ImageUtil;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.view.Dialog;
import com.yiwei.zhh.view.LoadingDialog;
import com.yiwei.zhh.view.uploadImage.UploadImgAdapter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by xsooy-pc on 17-12-8.
 */

public class IncomeIdentityActivity extends BaseActivity {


    @BindView(R.id.txt_add_image)
    TextView txtAddImage;
    @BindView(R.id.img_upload)
    ImageView imgUpload;
    @BindView(R.id.loan)
    TextView loan;
    @BindView(R.id.img_income1)
    ImageView imgIncome1;
    @BindView(R.id.img_income2)
    ImageView imgIncome2;
    @BindView(R.id.img_income3)
    ImageView imgIncome3;
    @BindView(R.id.img_income4)
    ImageView imgIncome4;
    @BindView(R.id.img_income5)
    ImageView imgIncome5;
    @BindView(R.id.view_img_bg)
    View viewImgBg;
    @BindView(R.id.img_add_image)
    ImageView imgAddImage;
    private Unbinder unbinder;
    private int selectPoi;
    private static final int CAMERA_REQUEST_CODE = 2;
    private LoadingDialog loadingDialog;
    private boolean isReady = false;
    private UploadImgAdapter uploadImgAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_income_identity);
        baseInitView(this, "IncomeIdentityActivity", Const.ViewType.ACTIVITY);
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        setBackIntent();
        setTitleText("收入证明");
        loadingDialog = new LoadingDialog(this);
        Glide.with(this).load(R.mipmap.img_income1).diskCacheStrategy(DiskCacheStrategy.NONE).into(imgIncome1);
        Glide.with(this).load(R.mipmap.img_income2).diskCacheStrategy(DiskCacheStrategy.NONE).into(imgIncome2);
        Glide.with(this).load(R.mipmap.img_income3).diskCacheStrategy(DiskCacheStrategy.NONE).into(imgIncome3);
        Glide.with(this).load(R.mipmap.img_income4).diskCacheStrategy(DiskCacheStrategy.NONE).into(imgIncome4);
        Glide.with(this).load(R.mipmap.img_income5).diskCacheStrategy(DiskCacheStrategy.NONE).into(imgIncome5);
    }

    private void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }
            @Override
            public void superReject() {

            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    imgAddImage.setImageResource(R.mipmap.img_photo_add_white);
                    viewImgBg.setBackgroundColor(0x66111111);
                    txtAddImage.setTextColor(0xffffffff);
                    isReady = true;
                    imgUpload.setImageBitmap(file);
                }
            }
        });
    }


    public void getUploadImage(String img) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        UploadImageBean bean = new UploadImageBean();
        bean.img = img;
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    getSubmit(result.getUrl());
                });
    }

    public void getSubmit(String imgUrl) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        IncomeCertBean bean = new IncomeCertBean();
        bean.deal_id = PreferenceUtils.getString(this, "deal_id", "");
        bean.work_image = imgUrl;
        Api.api_service.getIncomeCert(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "上传成功").show();
                    EventBus.getDefault().post(new IncomeEvent());
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImageUtil.releaseImageViewResouce(imgUpload);
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @OnClick({R.id.loan, R.id.img_upload, R.id.img_income1, R.id.img_income2, R.id.img_income3, R.id.img_income4, R.id.img_income5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_upload:
                goToTakePhoto();
                break;
            case R.id.loan:
                if (isReady) {
                    getUploadImage(ImageUtil.Bitmap2StrByBase64(((BitmapDrawable) imgUpload.getDrawable()).getBitmap()));
                } else {
                    ToastUtil.makeText(this, "请上传图片").show();
                }
                break;
            case R.id.img_income1:
                selectPoi = R.mipmap.img_income1;
                Dialog.showBigIcon(this, selectPoi);
                break;
            case R.id.img_income2:
                selectPoi = R.mipmap.img_income2;
                Dialog.showBigIcon(this, selectPoi);
                break;
            case R.id.img_income3:
                selectPoi = R.mipmap.img_income3;
                Dialog.showBigIcon(this, selectPoi);
                break;
            case R.id.img_income4:
                selectPoi = R.mipmap.img_income4;
                Dialog.showBigIcon(this, selectPoi);
                break;
            case R.id.img_income5:
                selectPoi = R.mipmap.img_income5;
                Dialog.showBigIcon(this, selectPoi);
                break;

        }
    }
}
